﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IP.Core.IPCommon;
using IP.Core.IPData;
using IP.Core.IPUserService;

using BKIDS;
using BKIDS.CDBNames;
using BKIUS;
using System.Data;

public partial class QuanLyNhanSu_F205_QuanLyLichSuChucVuThanhVien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_2_cbo_thanhvien();
            load_2_cbo_chucvu();
            load_2_grv_lichsuthanhvien(-1);
        }
    }

    #region Members
    US_GD_LICH_SU_THANH_VIEN m_us_lichsuthanhvien;
    DS_GD_LICH_SU_THANH_VIEN m_ds_lichsuthanhvien;
    US_DM_THANH_VIEN m_us_dm_thanhvien;
    DS_DM_THANH_VIEN m_ds_dm_thanhvien;    
    #endregion

    #region Public Interfaces
    public string Mapping_hoten(string ip_str_id_thanhvien)
    {
        try
        {
            decimal v_i_id_thanhvien = CIPConvert.ToDecimal(ip_str_id_thanhvien);
            US_V_DM_THANH_VIEN m_us_v_dm_thanhvien = new US_V_DM_THANH_VIEN(v_i_id_thanhvien);
            return CIPConvert.ToStr(m_us_v_dm_thanhvien.strHO_VA_TEN);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    public string Mapping_tendangnhap(string ip_str_id_thanhvien)
    {
        try
        {
            decimal v_i_id_thanhvien = CIPConvert.ToDecimal(ip_str_id_thanhvien);
            US_V_DM_THANH_VIEN m_us_v_dm_thanhvien = new US_V_DM_THANH_VIEN(v_i_id_thanhvien);
            return CIPConvert.ToStr(m_us_v_dm_thanhvien.strTEN_DANG_NHAP);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }

    public string Mapping_chucvu(string ip_str_id_chucvu)
    {
        try
        {
            decimal v_i_id_chucvu = CIPConvert.ToDecimal(ip_str_id_chucvu);
            US_CM_DM_TU_DIEN m_us_cm_dm_tudien = new US_CM_DM_TU_DIEN(v_i_id_chucvu);            
            return CIPConvert.ToStr(m_us_cm_dm_tudien.strTEN_NGAN);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    #endregion

    #region Private Methods
    private void load_2_cbo_thanhvien()
    {
        US_DM_DOI_TUONG v_us_dm_doi_tuong = new US_DM_DOI_TUONG();
        DS_DM_DOI_TUONG v_ds_dm_doi_tuong = new DS_DM_DOI_TUONG();
        v_us_dm_doi_tuong.FillDataset(v_ds_dm_doi_tuong);
        m_cbo_tenthanhvien.Items.Add(new ListItem("ALL", "0"));
        for (int v_i = 0; v_i < v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows.Count; v_i++)
        {
            m_cbo_tenthanhvien.Items.Add(new ListItem(CIPConvert.ToStr(v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows[v_i][DM_DOI_TUONG.HO_VA_TEN_DEM]).Trim() + " " + CIPConvert.ToStr(v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows[v_i][DM_DOI_TUONG.TEN]).Trim(), CIPConvert.ToStr(v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows[v_i][DM_DOI_TUONG.ID])));
        }
   
    }

    private void load_2_cbo_chucvu()
    {
        DS_CM_DM_TU_DIEN m_ds_cm_dm_tudien = new DS_CM_DM_TU_DIEN();
        US_CM_DM_TU_DIEN m_us_cm_dm_tudien = new US_CM_DM_TU_DIEN();
        try
        {
            m_us_cm_dm_tudien.FillDataset(m_ds_cm_dm_tudien);
            if (m_ds_cm_dm_tudien.CM_DM_TU_DIEN != null && m_ds_cm_dm_tudien.CM_DM_TU_DIEN.Rows.Count > 0)
            {
                cbo_chuvu.DataSource = m_ds_cm_dm_tudien.CM_DM_TU_DIEN;
                cbo_chuvu.DataTextField = CM_DM_TU_DIEN.TEN_NGAN;
                cbo_chuvu.DataValueField = CM_DM_TU_DIEN.ID;
                cbo_chuvu.DataBind();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;
        }

    }

    private void load_2_grv_lichsuthanhvien(decimal ip_id_thanhvien)
    {
        m_us_lichsuthanhvien = new US_GD_LICH_SU_THANH_VIEN();
        m_ds_lichsuthanhvien = new DS_GD_LICH_SU_THANH_VIEN();
        try
        {
            m_ds_lichsuthanhvien.Clear();
            // Đổ dữ liệu từ US vào DS
            if (ip_id_thanhvien == -1)
                m_us_lichsuthanhvien.FillDataset(m_ds_lichsuthanhvien);
            else
                m_us_lichsuthanhvien.FillDataset(m_ds_lichsuthanhvien, "WHERE ID_THANH_VIEN =" + ip_id_thanhvien);            
            // Nếu dữ liệu trống
            if (m_ds_lichsuthanhvien.GD_LICH_SU_THANH_VIEN.Rows.Count == 0)
            {
                m_lbl_thong_bao.Text = "Chưa có lịch sử nào cho thành viên này";                                
            }
            m_grv_dm_doi_tuong.DataSource = m_ds_lichsuthanhvien.GD_LICH_SU_THANH_VIEN;
            m_grv_dm_doi_tuong.DataBind();            
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;

        }
    }

    private void from_2_object(US_GD_LICH_SU_THANH_VIEN ob_us_lichsutv)
    {
        try
        {
            ob_us_lichsutv.dcID = CIPConvert.ToDecimal(m_btt_capnhat.ToolTip);
            ob_us_lichsutv.dcID_THANH_VIEN = CIPConvert.ToDecimal(m_cbo_tenthanhvien.SelectedValue);
            ob_us_lichsutv.dcID_CHUC_VU = CIPConvert.ToDecimal(cbo_chuvu.SelectedValue);
            ob_us_lichsutv.datNGAY_BAT_DAU = CIPConvert.ToDatetime(m_dat_batdau.Text);
            ob_us_lichsutv.datNGAY_KET_THUC = CIPConvert.ToDatetime(m_dat_ketthuc.Text);
            ob_us_lichsutv.strGHI_CHU = CIPConvert.ToStr(m_txt_ghichu.Text);
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;

        }    
    }

    private void delete_row_thanh_toan(int ip_i_id_thanh_vien)
    {
        // Lấy được ID thanh tóan
        decimal v_dc_id_thanh_vien = CIPConvert.ToDecimal(m_grv_dm_doi_tuong.DataKeys[ip_i_id_thanh_vien].Value);
        m_us_lichsuthanhvien.dcID = v_dc_id_thanh_vien;
        // Xóa GD_THANH_TOAN
        m_us_lichsuthanhvien.DeleteByID(v_dc_id_thanh_vien);
        m_lbl_thong_bao.Text = "Xóa bản ghi thành công";
        // Load lại dữ liệu
        load_2_grv_lichsuthanhvien(CIPConvert.ToDecimal(m_cbo_tenthanhvien.SelectedValue));        
    }

    private void load_data_2_us_by_id_and_show_on_form(int ip_i_lS_thanh_vien_selected)
    {
        try
        {
            decimal v_dc_id_thanh_vien = CIPConvert.ToDecimal(m_grv_dm_doi_tuong.DataKeys[ip_i_lS_thanh_vien_selected].Value);
            m_btt_capnhat.ToolTip = CIPConvert.ToStr(v_dc_id_thanh_vien);
            US_GD_LICH_SU_THANH_VIEN v_us_gd_lich_su_tv = new US_GD_LICH_SU_THANH_VIEN(v_dc_id_thanh_vien);            
            // Load data to form 
            m_cbo_tenthanhvien.SelectedValue = CIPConvert.ToStr(v_us_gd_lich_su_tv.dcID_THANH_VIEN);
            cbo_chuvu.SelectedValue = CIPConvert.ToStr(v_us_gd_lich_su_tv.dcID_CHUC_VU);
            m_dat_batdau.Text = CIPConvert.ToStr(v_us_gd_lich_su_tv.datNGAY_BAT_DAU,"dd/MM/yyy");
            m_dat_ketthuc.Text = CIPConvert.ToStr(v_us_gd_lich_su_tv.datNGAY_KET_THUC, "dd/MM/yyy");
            m_txt_ghichu.Text = CIPConvert.ToStr(v_us_gd_lich_su_tv.strGHI_CHU);
        }
        catch (Exception v_e)
        {

            throw v_e;
        }

    }

    #endregion

    #region Events
    protected void m_cbo_tenthanhvien_SelectedIndexChanged(object sender, EventArgs e)
    {
        DS_V_DM_THANH_VIEN m_ds_v_dm_thanhvien = new DS_V_DM_THANH_VIEN();
        try
        {
            if (m_cbo_tenthanhvien.SelectedValue == "0")
            {
                lab_chu_vu.Text = "";
                lab_tendangnhap.Text = "";
                lab_ngayvaonhom.Text = "";
                load_2_grv_lichsuthanhvien(0);
            }
            else
            {
                
                decimal v_i_id_thanhvien = CIPConvert.ToDecimal(m_cbo_tenthanhvien.SelectedValue);
                US_V_DM_THANH_VIEN m_us_v_dm_thanhvien = new US_V_DM_THANH_VIEN(v_i_id_thanhvien);
                lab_chu_vu.Text = CIPConvert.ToStr(m_us_v_dm_thanhvien.strTEN_CHUC_VU);
                lab_tendangnhap.Text = CIPConvert.ToStr(m_us_v_dm_thanhvien.strTEN_DANG_NHAP);
                lab_ngayvaonhom.Text = CIPConvert.ToStr(m_us_v_dm_thanhvien.datNGAY_VAO_ELEARNING, "0:dd/MM/yyyy");
                load_2_grv_lichsuthanhvien(v_i_id_thanhvien);
            }
            
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;
        }
    }

    protected void m_btt_taomoi_Click(object sender, EventArgs e)
    {
        try
        {
            if (m_cbo_tenthanhvien.SelectedValue != "0" && m_dat_batdau.Text != "" && m_dat_ketthuc.Text != "")
            {
                if (m_btt_capnhat.ToolTip != "") return;
                from_2_object(m_us_lichsuthanhvien);
                m_us_lichsuthanhvien.Insert();
                load_2_grv_lichsuthanhvien(CIPConvert.ToDecimal(m_cbo_tenthanhvien.SelectedValue));
                m_dat_batdau.Text = "";
                m_dat_ketthuc.Text = "";
                m_txt_ghichu.Text = "";
            }
            else
            {
                //nhớ using Ip.Common
                m_lbl_lois.Text = "Bạn chưa điềm đủ các thông tin cần thiết";
                m_lbl_lois.Visible = true;
            }
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;
        }
    }

    protected void m_btt_capnhat_Click(object sender, EventArgs e)
    {
        try
        {
            if (m_cbo_tenthanhvien.SelectedValue != "0" && m_dat_batdau.Text != "" && m_dat_ketthuc.Text != "")
            {
                if (m_btt_capnhat.ToolTip == "")
                {
                    m_lbl_lois.Text = "Bạn chưa chọn một";
                    m_lbl_lois.Visible = true;
                    return;
                }
                from_2_object(m_us_lichsuthanhvien);
                m_us_lichsuthanhvien.Update();
                load_2_grv_lichsuthanhvien(CIPConvert.ToDecimal(m_cbo_tenthanhvien.SelectedValue));
                m_dat_batdau.Text = "";
                m_dat_ketthuc.Text = "";
                m_txt_ghichu.Text = "";
                m_btt_taomoi.Enabled = true;
                m_btt_capnhat.Enabled = false;
            }
            else
            {
                //nhớ using Ip.Common
                m_lbl_lois.Text = "Bạn chưa điềm đủ các thông tin cần thiết";
                m_lbl_lois.Visible = true;
            }
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;
        }
    }

    protected void m_btt_xoatrang_Click(object sender, EventArgs e)
    {
        m_cbo_tenthanhvien.SelectedValue = "0";
        lab_chu_vu.Text = "";
        lab_ngayvaonhom.Text = "";
        lab_tendangnhap.Text = "";
        m_dat_batdau.Text = "";
        m_dat_ketthuc.Text = "";
        m_txt_ghichu.Text = "";
        m_lbl_lois.Visible = false;
        m_lbl_thong_bao.Visible = false;
        m_btt_taomoi.Enabled = true;
        m_btt_capnhat.Enabled = false;
    }

    protected void m_grv_dm_doi_tuong_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            delete_row_thanh_toan(e.RowIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    protected void m_grv_dm_doi_tuong_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            m_grv_dm_doi_tuong.PageIndex = e.NewPageIndex;
            load_2_grv_lichsuthanhvien(CIPConvert.ToDecimal(m_cbo_tenthanhvien.SelectedValue));
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    
    protected void m_grv_dm_doi_tuong_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        try
        {            
            m_btt_taomoi.Enabled = false;
            m_btt_capnhat.Enabled = true;
            load_data_2_us_by_id_and_show_on_form(e.NewSelectedIndex);
        }
        catch (Exception V_e)
        {
            CSystemLog_301.ExceptionHandle(this, V_e);
        }
    }

    #endregion

}